﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace Dune2013
{
    class Protector : Actor
    {
        private Actor target = null;
        public Protector(Actor target)
        {
            this.rangoRepulsion = 200f;
            this.target = target;
            position = new Vector2(100,100);
            speed = StaticStuff.rapidezProtector;
            maxTurnSpeed = .1f;
            this.tex = StaticStuff.getTextureById(StaticStuff.tiposTexturas.PROTECTOR);
        }

        public override void update(int dTime)
        {
            if (isThereHelicopter())
            {
                intercept(); 
            }
            else {
                justFollow();
            }
            base.update(dTime);
        }



        private void intercept() {
            Vector2 direccion = getDirection();
            Vector2 totalForce = seek(target.position + (50 * direccion));
            
            tryOrientation(totalForce);
        }

        private Vector2 getDirection() {
            Helicoptero h = getHelicopteroMasCercano();
            Vector2 direccion;

            direccion = h.position - target.position;
            direccion.Normalize();
            return direccion;
        }

        private Helicoptero getHelicopteroMasCercano() {
            ActorManager am = ActorManager.getInstance();
            Vector2 distance = new Vector2(0,0);
            Helicoptero ans = null;
            foreach (Actor h in am.actors)
            {
                if (typeof(Helicoptero) == h.GetType())
                {
                    if (ans == null)
                    {
                        ans = (Helicoptero)h;
                    }
                    else { 
                        if( ((target.position - h.position).LengthSquared()) < ((target.position - ans.position).LengthSquared()) ){
                            ans = (Helicoptero)h;
                        }
                    }
                }
            }
            return ans;
        }
        
        
        private void justFollow() {
            speed = StaticStuff.rapidezHelicoptero + 3;
            Vector2 totalForce = seek(target);
            totalForce += repulsion();
            if (totalForce.LengthSquared() == 0)
            {
                totalForce += flee(target);
            }
            tryOrientation(totalForce);
        }

        public override Vector2 repulsion()
        {
            ActorManager actorManager = ActorManager.getInstance();
            Vector2 totalRepulsion = new Vector2(0, 0);

            if (target != this && (this.position - target.position).LengthSquared() < rangoRepulsion * rangoRepulsion)
            {
                Vector2 distancia = this.position - target.position;
                float longitud = distancia.Length();
                distancia.Normalize();
                totalRepulsion = -1*seek(target);
            }
            
            return totalRepulsion;
        }

        private bool isThereHelicopter() {
            ActorManager am = ActorManager.getInstance();
            foreach (Actor h in am.actors)
            {
                if (typeof(Helicoptero) == h.GetType())
                {
                    return true;
                }
            }
            return false;
        }

    }
}
